package com.topscomm.mms.util;

import com.topscomm.basic.util.SQLAdapter;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;

import java.util.Map;

/**
 * 基础数据处理
 *
 * @description:
 * @author: donghang
 * @date: 2021年4月10日上午8:43:45
 * @modify:
 */
public class BaseDataUtil {
	/**
	 * 部门拆分，得到是否在上级部门中
	 *
	 * @description:
	 * @param colname
	 * @param deptautocode
	 * @param spiltstring
	 * @return
	 * @author: donghang
	 * @date: 2021年4月10日上午9:48:32
	 * @modify:
	 */
	public static String createWhereDeptCheckInclude(String colname, String deptautocode, String spiltstring) {
		StringBufferProxy sql = new StringBufferProxy();
		if (StringUtil.isEmpty(colname) || StringUtil.isEmpty(deptautocode))
			return "1=1";
		int start_num = 0;
		if (!deptautocode.trim().contains("_"))
			start_num = 2;
		else
			start_num = deptautocode.indexOf("_") + 3;
		String[] params = { "'" + spiltstring + "'", colname, "'" + spiltstring, "'" };
		for (int i = start_num; i <= deptautocode.length(); i = i + 2) {
			sql.appendSingle("or {0} like '%{1}%' ", SQLAdapter.concat(params),
					spiltstring + deptautocode.substring(0, i) + spiltstring);
		}
		String whereSql = "(" + sql.toString().substring(2) + ")";
		return whereSql;
	}

	/**
	 * 查询部门是否在里面
	 *
	 * @description:
	 * @param deptcodes
	 * @param deptautocode
	 * @return
	 * @author: donghang
	 * @date: 2021年4月13日下午3:51:42
	 * @modify:
	 */
	public static boolean queryDeptInclude(String deptcodes, String deptautocode) {
		if (StringUtil.isEmpty(deptcodes) || StringUtil.isEmpty(deptautocode))
			return false;
		deptcodes = "," + deptcodes + ",";
		int start_num = 0;
		if (!deptautocode.trim().contains("_"))
			start_num = 2;
		else
			start_num = deptautocode.indexOf("_") + 3;
		for (int i = start_num; i <= deptautocode.length(); i = i + 2) {
			String splitCode = "," + deptautocode.substring(0, i) + ",";
			if (deptcodes.contains(splitCode)) {
				return true;
			}
		}
		return false;
	}

	/**
	 *
	 * @description: 去掉参数中的空格
	 * @param
	 * @author: zhangqianbo
	 * @modify:
	 */
	public static void filterKeysSpace(Map<String, Object> paraMap, String[] keys) {
		if (keys == null || keys.length == 0) {
			return;
		}
		for (String key : keys) {
			if (!paraMap.containsKey(key)) {
				continue;
			}
			String value = ConvertUtil.convertToString(paraMap.get(key));
			if (!StringUtil.isEmpty(value)) {
				value = value.trim();
				paraMap.put(key, value);
			} else {
				paraMap.remove(key);
			}
		}
	}
}
